package artid.com.arttogo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.umeng.socialize.utils.Log;

import artid.com.arttogo.R;
import artid.com.arttogo.bean.Registered;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.utils.Config;
import artid.com.arttogo.utils.MySharedPreferences;

public class RegisterInputActivity extends BaseActivity implements
		OnClickListener {
	private TextView mComplete;
	private Intent mIntent;
	private TextView info, haveCode;
	private EditText pw, npw, code;
	private String phone, name;
	MySharedPreferences share;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_input);
		initView();
	}

	private void initView() {
		mContext = this;
		mBack = (ImageView) findViewById(R.id.back_btn);

		mTittle = (TextView) findViewById(R.id.tittle_text);
		info = (TextView) findViewById(R.id.tv_info);
		haveCode = (TextView) findViewById(R.id.have_code);

		pw = (EditText) findViewById(R.id.et_pw);
		npw = (EditText) findViewById(R.id.et_pw_new);
		code = (EditText) findViewById(R.id.et_code);

		phone = getIntent().getStringExtra("phone");
		name = getIntent().getStringExtra("name");
		info.setText("验证码已经发到您的手机" + phone + "中");
		mBack.setOnClickListener(this);
		mTittle.setText("注册");
		mComplete = (TextView) findViewById(R.id.complete);
		mComplete.setOnClickListener(this);
		timer.start();
	}

	private CountDownTimer timer = new CountDownTimer(60000, 1000) {

		@Override
		public void onTick(long millisUntilFinished) {
			haveCode.setText((millisUntilFinished / 1000) + "秒后可重发");
			haveCode.setBackgroundResource(R.drawable.btn_hui);
			haveCode.setClickable(false);
		}

		@Override
		public void onFinish() {
			haveCode.setEnabled(true);
			haveCode.setBackgroundResource(R.drawable.btn_orange);
			haveCode.setClickable(true);
			haveCode.setText("获取验证码");

		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.complete:
			complete();

			break;
			//
		case R.id.back_btn:
			finish();
			break;

		default:
			break;
		}
	}

	/**
	 * 
	 */
	private void complete() {
		// TODO Auto-generated method stub
		if (TextUtils.isEmpty(code.getText().toString())) {
			Toast.makeText(mContext, "验证码不能为空", 2).show();
			return;
		} else if (TextUtils.isEmpty(pw.getText().toString())) {
			Toast.makeText(mContext, "密码不能为空", 2).show();
			return;
		} else if (TextUtils.isEmpty(npw.getText().toString())) {
			Toast.makeText(mContext, "请再次输入密码", 2).show();
			return;
		} else if (!pw.getText().toString().equals(npw.getText().toString())) {
			Toast.makeText(mContext, "两次输入密码不一致", 2).show();
			return;
		}
		VolleyBasic.getInstance(RegisterInputActivity.this).post(Cache.Registered,
				VolleyMap.getInstance().Registered(phone, name, code.getText().toString(),  pw.getText().toString(), "1", "1"), Registered.class,
				new VolleyCallback() {

			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				final Registered registered  = (Registered)vd.getData();
				Config.showTip(mContext, "注册成功！");
				share = MySharedPreferences.getMySharedPreferences(RegisterInputActivity.this);
				share.setValue("login", phone);
				
				new Thread(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						//环信注册
						try {
							// 调用sdk注册方法
							EMClient.getInstance().createAccount(registered.getUid(), "yjbgp2016");
							runOnUiThread(new Runnable() {
								public void run() {
									Log.d("log",registered.getUid()+"：环信注册成功");
								}
							});
						} catch (HyphenateException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							Log.d("log", AppUtil.getUser().getUid()+"：环信注册失败");
						}
					}
				}).start();
				mIntent = new Intent(mContext, LoginActivity.class);
				mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mIntent);
				finish();
			}

			@Override
			public void onError(VolleyError volleyError) {
				// TODO Auto-generated method stub

			}
		});
	}


}
